package com.atguigu.mapreduce.shiyan;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class MergeFiles {

    public static class FileAMapper extends Mapper<Object, Text, Text, Text> {
        private Text outKey = new Text();
        private Text outValue = new Text();
        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split(" ");
            if (parts.length == 2) {
                outKey.set(parts[0]);
                outValue.set("A:" + parts[1]);
                context.write(outKey, outValue);
            }
        }
    }

    public static class FileBMapper extends Mapper<Object, Text, Text, Text> {
        private Text outKey = new Text();
        private Text outValue = new Text();
        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split(" ");
            if (parts.length == 2) {
                outKey.set(parts[0]);
                outValue.set("B:" + parts[1]);
                context.write(outKey, outValue);
            }
        }
    }

    public static class MergeReducer extends Reducer<Text, Text, Text, Text> {
        private Text result = new Text();
        @Override
        public void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            Set<String> uniqueValues = new HashSet<>();
            for (Text value : values) {
                uniqueValues.add(value.toString().substring(2));
            }
            for (String value : uniqueValues) {
                result.set(value);
                context.write(key, result);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME","root");
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://master:9000");
        Job job = Job.getInstance(conf, "merge files");
        job.setJarByClass(MergeFiles.class);
        job.setReducerClass(MergeReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        MultipleInputs.addInputPath(job, new Path("/user/hadoop/input/2.txt"), TextInputFormat.class, FileAMapper.class);
        MultipleInputs.addInputPath(job, new Path("/user/hadoop/input/3.txt"), TextInputFormat.class, FileBMapper.class);
        FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/output5"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
